package com.example.books.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.books.R;
import com.example.books.data.Book;
import com.example.books.data.Collection;
import com.example.books.tools.GetResoures;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class BookAdapter extends BaseAdapter {
    List<Book> all = new ArrayList<>();
    Context context;
    Long[] ib;

    public BookAdapter(List<Book> all, Context context,Long[] i) {
        this.all = all;
        this.context = context;
        this.ib=i;
    }

    @Override
    public int getCount() {
        return all.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return all.get(i).getId();
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.layout, null);
            viewHolder=new ViewHolder();
            viewHolder.image_book = (ImageView) view.findViewById(R.id.image_book);
            viewHolder.author_book = (TextView) view.findViewById(R.id.author_book);
            viewHolder.name_book = (TextView) view.findViewById(R.id.name_book);
            viewHolder.imageView = (ImageView) view.findViewById(R.id.imgColletion);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        for (int a=0;a<ib.length;a++){
            if(ib[a]==all.get(i).getId()){
                viewHolder.imageView.setSelected(true);
                break;
            }
        }
        viewHolder.image_book.setImageBitmap(GetResoures.GetImage(context,all.get(i).getBookPicture()));
        viewHolder.author_book.setText(all.get(i).getBookAuthor());
        viewHolder.name_book.setText(all.get(i).getBoolkName());
        return view;
    }

    public class ViewHolder {
        ImageView image_book;
        TextView author_book;
        TextView name_book;
        ImageView imageView;

    }
}
